package cn.lctvu.servlet.admin;

import cn.lctvu.bean.User;
import cn.lctvu.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

@WebServlet("/admin/AddManagerServlet")
public class AddManagerServlet extends HttpServlet {
    private UserService userService=new UserService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        request.setCharacterEncoding("utf-8");
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String repassword=request.getParameter("password_confirm");
        String email=request.getParameter("email");
        if(!password.equals(repassword)){
            out.println("<script language='javascript'>alert('密码和确认密码不一致');document.location.href='/admin/manageradd.jsp';</script>");
            return;
        }
        User user=new User(username,password,email,new Date());
        boolean b=userService.addUser(user);
        if(b){
            out.println("<script language='javascript'>alert('添加成功');document.location.href='/admin/AdminServlet';</script>");
        }else {
            out.println("<script language='javascript'>alert('添加失败');document.location.href='/admin/AdminServlet';</script>");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request,response);
    }
}
